
package control.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.CharacterBEAN;
import bean.EquipmentBEAN;
import dao.CharacterDAO;
import dao.EquipmentDAO;
import dao.NetworkDAO;
import dao.ObjectDAO;

/**
 * Servlet implementation class User
 */
public class User extends HttpServlet {
	private static final long	serialVersionUID	= 1L;
	private CharacterDAO		characterDAO;
	private ObjectDAO			objectDAO;
	private NetworkDAO			networkDAO;
	private EquipmentDAO		equipmentDAO;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public User() {
		super();
	}

	public void init( ServletConfig config ) throws ServletException {
		characterDAO = (CharacterDAO) config.getServletContext().getAttribute(
				"CharacterDAO" );
		equipmentDAO = (EquipmentDAO) config.getServletContext().getAttribute( "EquipmentDAO" );
		objectDAO = (ObjectDAO) config.getServletContext().getAttribute(
				"ObjectDAO" );
		networkDAO = (NetworkDAO) config.getServletContext().getAttribute(
				"NetworkDAO" );
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		String action = request.getParameter( "action" );
		HttpSession session = request.getSession();
		if( session.getAttribute( "ID" ) != null ) {
			int accountID = (Integer) session.getAttribute( "ID" );
			CharacterBEAN characterBEAN = characterDAO
					.getCharacterByID( accountID );
			request.setAttribute( "character", characterBEAN );
			if( action.startsWith( "goToProfile" ) ) {
				CharacterBEAN profileChar = characterDAO
						.getCharacterByID( Integer.parseInt( action
								.substring( 11 ) ) );
				request.setAttribute( "profileChar", profileChar );
				if( profileChar.getObject() != 0 ) {
					request.setAttribute( "profileObject", objectDAO
							.getObjectByID( profileChar.getObject() ) );
				}
				if( profileChar.getNetworkID() != 0 ) {
					request.setAttribute( "profileNetwork", networkDAO
							.getNetworkByID( profileChar.getNetworkID() ) );
				}
				goTo( "WEB-INF/logged/profilePage.jsp", request, response );
				return;
			} else if( action.equals( "userPage" ) ) {
				CharacterBEAN profileChar = characterDAO
						.getCharacterByID( accountID );
				EquipmentBEAN equipment = equipmentDAO.getEquipmentByID( accountID );
				request.setAttribute( "equipment", equipment );
				request.setAttribute( "profileChar", profileChar );
				if( profileChar.getObject() != 0 ) {
					request.setAttribute( "profileObject", objectDAO
							.getObjectByID( profileChar.getObject() ) );
				}
				if( profileChar.getNetworkID() != 0 ) {
					request.setAttribute( "profileNetwork", networkDAO
							.getNetworkByID( profileChar.getNetworkID() ) );
				}
				goTo( "WEB-INF/logged/userPage.jsp", request, response );
				return;
			}
		} else {
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		String action = request.getParameter( "action" );
		HttpSession session = request.getSession();
		if( session.getAttribute( "ID" ) != null ) {
			int accountID = (Integer) session.getAttribute( "ID" );
			CharacterBEAN characterBEAN = characterDAO
					.getCharacterByID( accountID );
			request.setAttribute( "character", characterBEAN );
			if( action.equals( "findUser" ) ) {
				boolean startsWith = false;
				if( request.getParameter( "find" ).endsWith( "*" ) ) {
					startsWith = true;
				}
				ArrayList< CharacterBEAN > list = characterDAO
						.getCharacterByName( request.getParameter( "find" ),
								startsWith );
				request.setAttribute( "userList", list );
				goTo( "WEB-INF/logged/userList.jsp", request, response );
				return;
			} else {
			}
		}
	}

	private void goTo( String action, HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		try {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher( action );
			dispatcher.forward( request, response );
		} catch( Exception e ) {
			e.printStackTrace();
		}
	}
}
